package com.topideal.supplychain.hades.schedule;//package com.topideal.supplychain.hades.schedule;
//
//import java.lang.reflect.InvocationTargetException;
//import java.text.SimpleDateFormat;
//import java.util.Arrays;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//import org.apache.commons.lang.StringUtils;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//import com.topideal.supplychain.framework.common.DateUtil;
//import com.topideal.supplychain.framework.common.DateUtil.AddpartEnum;
//import com.topideal.supplychain.framework.common.SpringUtils;
//import com.topideal.supplychain.framework.exception.BusinessException;
//import com.topideal.supplychain.hades.common.EnumConstants;
//import com.topideal.supplychain.hades.master.model.Carrier;
//import com.topideal.supplychain.hades.master.service.CarrierService;
//import com.topideal.supplychain.hades.task.model.TskSubscribeReq;
//import com.topideal.supplychain.hades.task.service.TskSubscribeReqService;
//import com.topideal.supplychain.hades.util.LogUtils;
//
//
///**
// * 发送订阅订单物流日志请求到承运商
// * 
// * @author zzh
// *
// */
//@Component("subscribeOrderLog2TplJob")
//public class SubscribeOrderLog2TplJob {
//    private Logger logger;
//    private Integer maxNum;
//    private Integer intervalMinute;
//    private Integer maxCallCount;
//
//    @Autowired
//    private TskSubscribeReqService subscribeReqService;
//
//    @Autowired
//    private CarrierService carrierService;
//
//    public SubscribeOrderLog2TplJob() {
//        logger = Logger.getLogger(SubscribeOrderLog2TplJob.class);
//        this.maxCallCount = Integer.valueOf(5);
//        this.intervalMinute = Integer.valueOf(10);
//
//    }
//
//    public void sendSubscribe2Tpl() {
//        logger.info("begin execute subscribe job..");
//        long t0 = System.nanoTime();
//        boolean isFinished = false;
//        do {
//            List<TskSubscribeReq> unProcessedData = findUnProcessedData();
//            if (unProcessedData == null) {
//                break;
//            }
//
//            for (Iterator<TskSubscribeReq> i$ = unProcessedData.iterator(); i$.hasNext();) {
//                TskSubscribeReq t = i$.next();
//                processSingleData(t);
//            }
//
//            isFinished = isFinished(unProcessedData);
//        } while (!(isFinished));
//
//        long dt = System.nanoTime() - t0;
//        String duration = StringUtils.leftPad((dt / 1000000.0D) + " ms", 11);
//        logger.info("end execute subscribe job, cost " + duration);
//    }
//
//    protected boolean isFinished(List<?> list) {
//        return CollectionUtils.isEmpty(list);
//    }
//
//    private List<TskSubscribeReq> findUnProcessedData() {
//
//        Map<String, Object> param = new HashMap<String, Object>();
//        // param.put("carrierIdList",Arrays.asList(new Long[] { carrierId }));
//        param.put("maxRows", getMaxNum());
//        param.put("dealCount", getMaxCallCount());
//        param.put("stateList",
//                Arrays.asList(new Integer[] {EnumConstants.SubscribeState.INIT.getValue(),
//                        EnumConstants.SubscribeState.SUBSCRIBE_FAIL.getValue()}));
//        Date intervalDate = DateUtil.dateAdd(AddpartEnum.MI, DateUtil.getNowTime(),
//                -this.intervalMinute.intValue());
//        param.put("intervalDate", intervalDate);
//        List<TskSubscribeReq> subscribeReqs =
//                this.subscribeReqService.findLimitSubscribeReqList(param);
//
//        return subscribeReqs;
//    }
//
//    protected void processSingleData(TskSubscribeReq subscribeReq) {
//        try {
//            if (subscribeReq.getCarrierId() == null) {
//                return;
//            }
//            Carrier carrier = this.carrierService.get(subscribeReq.getCarrierId());
//            if (carrier == null) {
//                return;
//            }
//
//            Object srv = SpringUtils.getBean(carrier.getToSystem() + "ServiceImpl");
//            if (srv == null) {
//                throw new BusinessException("订阅请求找不到对应的目标系统服务");
//            }
//            srv.getClass()
//                    .getMethod("sendSubscribeReq",
//                            new Class[] {TskSubscribeReq.class, Carrier.class})
//                    .invoke(srv, new Object[] {subscribeReq, carrier});
//            updateSubscribeReqSuccess(subscribeReq);
//        } catch (Exception e) {
//            String errorMsg = null;
//            if (e instanceof InvocationTargetException) {
//                InvocationTargetException e1 = (InvocationTargetException) e;
//                if ((null != e1) && (null != e1.getTargetException()))
//                    errorMsg = e1.getTargetException().getMessage();
//                else
//                    errorMsg = "message异常信息为空";
//            } else {
//                errorMsg = e.getMessage();
//            }
//            logger.error("发送订阅请求信息失败, subscribeReq=" + subscribeReq.toString(), e);
//            String errorLog = "发送订阅请求信息失败, Exception:  " + errorMsg;
//            updateSubscribeReqException(subscribeReq, errorLog);
//        }
//    }
//
//    protected void updateSubscribeReqSuccess(TskSubscribeReq subscribeReq) {
//        TskSubscribeReq condition = new TskSubscribeReq();
//        condition.setId(subscribeReq.getId());
//        condition.setDealCount(Integer.valueOf(subscribeReq.getDealCount().intValue() + 1));
//        condition.setState(EnumConstants.SubscribeState.SUBSCRIBE_SUCCESS.getValue());
//        condition.setUpdateTime(new Date());
//        this.subscribeReqService.update(condition);
//    }
//
//    protected void updateSubscribeReqException(TskSubscribeReq subscribeReq, String errorMsg) {
//        TskSubscribeReq condition = new TskSubscribeReq();
//        condition.setId(subscribeReq.getId());
//        condition.setDealCount(Integer.valueOf(subscribeReq.getDealCount().intValue() + 1));
//        condition.setState(EnumConstants.SubscribeState.SUBSCRIBE_FAIL.getValue());
//        condition.setUpdateTime(new Date());
//        condition.setErrMsg(LogUtils.subErrorMessage(errorMsg));
//        this.subscribeReqService.update(condition);
//    }
//
//    public void onlyRetry(TskSubscribeReq subscribeReq) {
//        this.processSingleData(subscribeReq);
//    }
//
//    public Integer getMaxNum() {
//        return maxNum;
//    }
//
//    public void setMaxNum(Integer maxNum) {
//        this.maxNum = maxNum;
//    }
//
//    public Integer getMaxCallCount() {
//        return maxCallCount;
//    }
//
//    public void setMaxCallCount(Integer maxCallCount) {
//        this.maxCallCount = maxCallCount;
//    }
//
//    public static void main(String[] args) {
//        Calendar c = Calendar.getInstance();
//        c.setTime(new Date());
//        c.add(12, -10);
//        Date intervalDate = c.getTime();
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(intervalDate));
//    }
//}
